package org.jeecg.modules.bookmarks.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.bookmarks.entity.Bookmark;
import org.jeecg.modules.bookmarks.service.IBookmarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 功能描述
 *
 * @author: scott
 * @date: 2022年05月12日 5:00 下午
 */
@Slf4j
@RestController
@RequestMapping("/bus/bookmark")
@Api(tags = "三维地图书签管理")
public class BookmarkController {

    /**
     * 注入书签服务
     */
    @Autowired
    private IBookmarkService bookmarkService;


    /**
     * 根据id获取三维地图书签信息
     *
     * @param id:markid
     * @return Result<Bookmark>
     */
    @ApiOperation(value = "根据id获取三维地图书签信息")
    @RequestMapping(value = "/getBookmarkById", method = RequestMethod.GET)
    public Result<Bookmark> getBookmarkById(@RequestParam(name = "id") String id) {

        Result<Bookmark> result = new Result<>();
        Bookmark bookmark = bookmarkService.getBookmarkById(id);

        if (bookmark == null) {
            result.error500("当前id书签不存在！");
        } else {
            result.setResult(bookmark);
            result.setSuccess(true);
        }
        return result;

    }

    /**
     * 获取所有书签列表
     *
     * @return
     */
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取书签列表")
    public Result<List<Bookmark>> list() {
        Result<List<Bookmark>> result = new Result<>();

        List<Bookmark> list = bookmarkService.listAll();

        if (list == null) {
            result.error500("书签列表为空");
        } else {
            result.setResult(list);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 创建书签
     *
     * @param jsonObject：书签json对象
     * @return Bookmark
     */
    @ApiOperation("创建书签")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<Bookmark> add(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Result<Bookmark> result = new Result<>();
        try {
            Bookmark bookmark = JSON.parseObject(jsonObject.toJSONString(), Bookmark.class);
            // 获取当前用户
            String username = JwtUtil.getUserNameByToken(request);
            bookmark.setCreateTime(new Date());//设置创建时间
            bookmark.setCreateBy(username);
            bookmark.setUpdateTime(new Date());
            bookmark.setUpdateBy(username);
            bookmarkService.saveBookmark(bookmark);
            bookmark.setYxbz("Y");
            bookmark.setXybz("Y");
            result.success("添加成功！");
            result.setResult(bookmark);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败" + e.getMessage());
        }
        return result;
    }

    /**
     * 删除书签
     *
     * @param id      :书签id
     * @param request
     * @return
     */
    @ApiOperation("删除书签")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Result<Bookmark> delete(@RequestParam(name = "id") String id, HttpServletRequest request) {
        Result<Bookmark> result = new Result<>();
        try {
            Bookmark bookmark = bookmarkService.getBookmarkById(id);
            if (bookmark != null) {
                bookmark.setYxbz("N");
                // 获取当前用户
                String username = JwtUtil.getUserNameByToken(request);
                bookmark.setUpdateTime(new Date());
                bookmark.setUpdateBy(username);
                bookmarkService.editBookmark(bookmark);
                result.success("删除成功！");
                result.setResult(bookmark);

            } else {
                result.error500("操作失败");
            }


        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败" + e.getMessage());
        }
        return result;
    }

}
